package vanilla_technology.building;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;

import java.util.Iterator;
import java.util.List;

/**
 * Created by winston_wang on 2017/5/13.
 */
public class Buildings {
    @Building(triggerBlock = "minecraft:iron_block")
    public void buildingBreaker(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.ANVIL)) {
            player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_PISTON_EXTEND, SoundCategory.NEUTRAL, 0.5F, 0.4F);
            BlockPos newPos = new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ());
            IBlockState blockState = player.worldObj.getBlockState(newPos);
            blockState.getBlock().harvestBlock(player.worldObj, player, newPos, blockState, player.worldObj.getTileEntity(newPos), null);
            if (blockState.getBlock().getBlockHardness(null, null, null) != -1) {
                player.worldObj.setBlockToAir(newPos);
            }
        }
    }

    @Building(triggerBlock = "minecraft:redstone_block")
    public void buildingDigger(EntityPlayer player, BlockPos pos) {
        BlockPos l = new BlockPos(new BlockPos(pos.getX() + 1, pos.getY(), pos.getZ()));
        BlockPos r = new BlockPos(new BlockPos(pos.getX() - 1, pos.getY(), pos.getZ()));
        BlockPos u = new BlockPos(new BlockPos(pos.getX(), pos.getY(), pos.getZ() + 1));
        BlockPos d = new BlockPos(new BlockPos(pos.getX(), pos.getY(), pos.getZ() - 1));
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.DIAMOND_BLOCK)) {
            if (player.worldObj.getBlockState(l).getBlock().equals(Blocks.IRON_BARS)
                    && player.worldObj.getBlockState(r).getBlock().equals(Blocks.IRON_BARS)
                    && player.worldObj.getBlockState(u).getBlock().equals(Blocks.IRON_BARS)
                    && player.worldObj.getBlockState(d).getBlock().equals(Blocks.IRON_BARS)) {
                if (player.getHeldItemMainhand() != null
                        && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.REDSTONE))) {
                    player.getHeldItemMainhand().stackSize--;
                    player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_ANVIL_PLACE, SoundCategory.NEUTRAL, 0.5F, 0.4F);
                    for (int y = 0; y < 32; y++) {
                        for (int x = 0; x < 3; x++) {
                            for (int z = 0; z < 3; z++) {
                                BlockPos newPos = new BlockPos(pos.getX() - 1 + x, pos.getY() - 1 - y, pos.getZ() - 1 + z);
                                IBlockState blockState = player.worldObj.getBlockState(newPos);
                                blockState.getBlock().harvestBlock(player.worldObj, player, newPos, blockState, player.worldObj.getTileEntity(newPos), null);
                                if (blockState.getBlock().getBlockHardness(null, null, null) != -1) {
                                    player.worldObj.setBlockToAir(newPos);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Building(triggerBlock = "minecraft:redstone_block")
    public void buildingFireBreaker(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.GOLD_BLOCK)) {
            if (player.getHeldItemMainhand() != null
                    && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.MAGMA_CREAM))) {
                player.getHeldItemMainhand().stackSize--;
                player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_LAVA_POP, SoundCategory.NEUTRAL, 0.5F, 0.4F);
                BlockPos newPos = new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ());
                IBlockState blockState = player.worldObj.getBlockState(newPos);
                if (blockState.getBlock().getBlockHardness(null, null, null) != -1) {
                    Object obj = FurnaceRecipes.instance().getSmeltingResult(new ItemStack(blockState.getBlock()));
                    if (obj != null) {
                        player.worldObj.setBlockToAir(newPos);
                        EntityItem entityItem = new EntityItem(player.worldObj, newPos.getX(), newPos.getY(), newPos.getZ(), ((ItemStack) obj).copy());
                        entityItem.setDefaultPickupDelay();
                        player.worldObj.spawnEntityInWorld(entityItem);
                    } else {
                        blockState.getBlock().harvestBlock(player.worldObj, player, newPos, blockState, player.worldObj.getTileEntity(newPos), null);
                        player.worldObj.setBlockToAir(newPos);
                    }
                }
            }
        }
    }

    @Building(triggerBlock = "minecraft:redstone_block")
    public void buildingFireDigger(EntityPlayer player, BlockPos pos) {
        BlockPos l = new BlockPos(new BlockPos(pos.getX() + 1, pos.getY(), pos.getZ()));
        BlockPos r = new BlockPos(new BlockPos(pos.getX() - 1, pos.getY(), pos.getZ()));
        BlockPos u = new BlockPos(new BlockPos(pos.getX(), pos.getY(), pos.getZ() + 1));
        BlockPos d = new BlockPos(new BlockPos(pos.getX(), pos.getY(), pos.getZ() - 1));
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.EMERALD_BLOCK)) {
            if (player.worldObj.getBlockState(l).getBlock().equals(Blocks.NETHER_BRICK_FENCE)
                    && player.worldObj.getBlockState(r).getBlock().equals(Blocks.NETHER_BRICK_FENCE)
                    && player.worldObj.getBlockState(u).getBlock().equals(Blocks.NETHER_BRICK_FENCE)
                    && player.worldObj.getBlockState(d).getBlock().equals(Blocks.NETHER_BRICK_FENCE)) {
                if (player.getHeldItemMainhand() != null
                        && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.REDSTONE))
                        && player.getHeldItemMainhand().stackSize >= 2) {
                    player.getHeldItemMainhand().stackSize -= 2;
                    player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.NEUTRAL, 0.5F, 0.4F);
                    for (int y = 0; y < 64; y++) {
                        for (int x = 0; x < 3; x++) {
                            for (int z = 0; z < 3; z++) {
                                BlockPos newPos = new BlockPos(pos.getX() - 1 + x, pos.getY() - 1 - y, pos.getZ() - 1 + z);
                                IBlockState blockState = player.worldObj.getBlockState(newPos);
                                if (blockState.getBlock().getBlockHardness(null, null, null) != -1) {
                                    Object obj = FurnaceRecipes.instance().getSmeltingResult(new ItemStack(blockState.getBlock()));
                                    if (obj != null) {
                                        player.worldObj.setBlockToAir(newPos);
                                        EntityItem entityItem = new EntityItem(player.worldObj, pos.getX(), pos.getY() + 2, pos.getZ(), ((ItemStack) obj).copy());
                                        entityItem.setDefaultPickupDelay();
                                        player.worldObj.spawnEntityInWorld(entityItem);
                                    } else {
                                        blockState.getBlock().harvestBlock(player.worldObj, player, newPos, blockState, player.worldObj.getTileEntity(newPos), null);
                                        player.worldObj.setBlockToAir(newPos);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Building(triggerBlock = "minecraft:gold_block")
    public void buildingTNTTower(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.DISPENSER)) {
            if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.TNT)) {
                int check_x = 0;
                int check_z = 0;
                if (player.worldObj.getBlockState(new BlockPos(pos.getX() + 1, pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.IRON_BLOCK)) {
                    check_x = 5;
                } else if (player.worldObj.getBlockState(new BlockPos(pos.getX() - 1, pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.IRON_BLOCK)) {
                    check_x = -5;
                } else if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ() + 1)).getBlock().equals(Blocks.IRON_BLOCK)) {
                    check_z = 5;
                } else if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ() - 1)).getBlock().equals(Blocks.IRON_BLOCK)) {
                    check_z = -5;
                }
                player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_FIREWORK_LAUNCH, SoundCategory.NEUTRAL, 0.5F, 0.4F);
                player.worldObj.setBlockToAir(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ()));
                EntityTNTPrimed tnt = new EntityTNTPrimed(player.worldObj, pos.getX(), pos.getY() + 3, pos.getZ(), player);
                tnt.motionY = 2;
                tnt.motionX = check_x;
                tnt.motionZ = check_z;
                player.worldObj.spawnEntityInWorld(tnt);
            }
        }
    }

    @Building(triggerBlock = "minecraft:diamond_block")
    public void buildingElectromagneticTower(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.IRON_BARS)) {
            if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.GOLD_BLOCK)) {
                if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).getBlock().equals(Blocks.GOLD_BLOCK)) {
                    if (player.getHeldItemMainhand() != null
                            && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.GUNPOWDER))) {
                        player.getHeldItemMainhand().stackSize--;
                        player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_GLASS_BREAK, SoundCategory.NEUTRAL, 0.5F, 0.4F);
                        List list = player.worldObj.getEntitiesWithinAABB(EntityLiving.class, new AxisAlignedBB(pos.getX() - 30D, pos.getY() - 20D, pos.getZ() - 30D, pos.getX() + 30D, pos.getY() + 20D, pos.getZ() + 30D));
                        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
                            EntityLiving entity = (EntityLiving) iterator.next();
                            if (entity.equals(player)) {
                                continue;
                            }
                            entity.setHealth(entity.getHealth() - 5.0f);
                            entity.spawnExplosionParticle();

                        }
                    }
                }
            }
        }
    }

    @Building(triggerBlock = "minecraft:gold_block")
    public void buildingHeater(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.FURNACE)) {
            if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.STONE)) {
                if (player.getHeldItemMainhand() != null
                            && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.REDSTONE))
                        && player.getHeldItemMainhand().stackSize >= 2) {
                    player.getHeldItemMainhand().stackSize -= 2;
                    player.worldObj.setBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ()),Blocks.LAVA.getDefaultState());
                }
            }
        }
    }
    @Building(triggerBlock = "minecraft:iron_block")
    public void buildingPotionTower(EntityPlayer player, BlockPos pos) {
        if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).getBlock().equals(Blocks.DISPENSER)) {
            if (player.worldObj.getBlockState(new BlockPos(pos.getX(), pos.getY() + 2, pos.getZ())).getBlock().equals(Blocks.DIAMOND_BLOCK)) {
                if (player.getHeldItemMainhand() != null
                        && player.getHeldItemMainhand().isItemEqual(new ItemStack(Items.POTIONITEM))) {
                    player.getHeldItemMainhand().stackSize --;
                    player.inventory.addItemStackToInventory(new ItemStack(Items.GLASS_BOTTLE));
                    List list = player.worldObj.getEntitiesWithinAABB(EntityLiving.class, new AxisAlignedBB(pos.getX() - 30D, pos.getY() - 20D, pos.getZ() - 30D, pos.getX() + 30D, pos.getY() + 20D, pos.getZ() + 30D));
                    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
                        EntityLiving entity = (EntityLiving) iterator.next();
                        for (PotionEffect potioneffect : PotionUtils.getEffectsFromStack(player.getHeldItemMainhand()))
                        {
                            entity.addPotionEffect(new PotionEffect(potioneffect));
                        }
                    }
                    for (PotionEffect potioneffect : PotionUtils.getEffectsFromStack(player.getHeldItemMainhand()))
                    {
                        player.addPotionEffect(new PotionEffect(potioneffect));
                    }
                }
            }
        }
    }
}
